package models;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Salad extends Product {
	
	public Date creationDate;

	public int popularity;
	
	@OneToOne
	public User creator;
	
	//@ManyToMany
	//public List<Ingredient> ingredients;
	
	@Override
	public int getCalories() {
		int calories = 0;
		for (Ingredient ingredient : this.ingredients) {
			calories += ingredient.calories;
		}
		return calories;
	}

	@Override
	public double getPrice() {
		double price = 0;
		for (Ingredient ingredient : this.ingredients) {
			price += ingredient.price;
		}
		return price;
	}
	
	public abstract List<IngredientCategory> getIngredientCategories();

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result
				+ ((creationDate == null) ? 0 : creationDate.hashCode());
		result = prime * result + ((creator == null) ? 0 : creator.hashCode());
		result = prime * result
				+ ((ingredients == null) ? 0 : ingredients.hashCode());
		result = prime * result + popularity;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (!(obj instanceof Salad))
			return false;
		Salad other = (Salad) obj;
		if (creationDate == null) {
			if (other.creationDate != null)
				return false;
		} else if (!creationDate.equals(other.creationDate))
			return false;
		if (creator == null) {
			if (other.creator != null)
				return false;
		} else if (!creator.equals(other.creator))
			return false;
		if (ingredients == null) {
			if (other.ingredients != null)
				return false;
		} else if (!ingredients.equals(other.ingredients))
			return false;
		if (popularity != other.popularity)
			return false;
		return true;
	}
	
}
